<template>
    <div>
        <van-search
        v-model="news.value"
        show-action
        placeholder="海底两万里"
        shape="round"
        :@search="onSearch"
        id="mid"
        >
            
            <template #left>
                <div @click="onClickLeft" id="le">
                    <van-icon name="arrow-left" />
                </div>
            </template>
            <template #right-icon>
                <div @click="onClickButton">
                    <i class="iconfont icon-zhaoxiangji"></i>
                </div>
            </template>
            <template #action>
                <div @click="addNews()" id="ri" to="/search">搜索</div>
            </template>
        </van-search>
    </div>
</template>

<script>
import { ref } from 'vue';
import sha1 from "sha1";
    export default {
        setup() {
            const onClickLeft = () => history.back();
            const onSearch = (val) => Toast(val);
            const addNews = () => Toast(value.value);
            const value = ref('');
            return {
                onClickLeft,
                value,
            };
        },
        data() {
            return {
                list: [],
                news:{}
            }
        },
        mounted () {
            this.getNewsList();
            

        },
        methods: {
            getNewsList() {
                var app_id='A6107876068110';
                var now = Date.now();
                var appKey = sha1(app_id+"UZ"+"58EBAF35-D1F8-D16D-C10F-3EFF74B31665"+"UZ"+now)+"."+now
                let url="https://d.app3c.cn/mcm/api/SearchHistory";
                return this.axios({
                    "method":"get",
                    "url":url,
                "headers": {
                    "X-APICloud-AppId": app_id,
                    "X-APICloud-AppKey":appKey
                }
                }).then((res)=>{
                    this.list=res.data;
                })
            },
            addNews() {
            var app_id='A6107876068110';
            var now = Date.now();
            var appKey = sha1(app_id+"UZ"+"58EBAF35-D1F8-D16D-C10F-3EFF74B31665"+"UZ"+now)+"."+now
            let url="https://d.app3c.cn/mcm/api/SearchHistory"
            return this.axios({
                "method":"post",
                "url":url,
            "headers": {
                "X-APICloud-AppId": app_id,
                "X-APICloud-AppKey":appKey
            },
            "data":{
                title:this.news.value,
            }
            }).then(res=>{
                if(res.status==200){
                this.getNewsList();
                this.$router.go(0);
                }      
                else
                alert("插入失败");
                })
            },
        },
    };
</script>

<style lang="scss" scoped>
    div{
        #le{
            padding-right: 2vw;
        }
        #ri{
            padding-left: 2vw;
            padding-right: 1vw;
        }
        .iconfont{
            padding-right: 2vw;
            font-size: 5vw;
        }
    }
</style>